Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"crypto/sha256"
"fmt"
"unicode"
"ascend-common/api"
)
const (
maskLen = 2
)
func ReplacePrefix(source, prefix string) string {
if prefix == "" {
prefix = "****"
}
if len(source) <= maskLen {
return prefix
}
end := string([]rune(source)[maskLen:len(source)])
return prefix + end
}
func MaskPrefix(source string) string {
return ReplacePrefix(source, "")
}
func GetSha256Code(data []byte) []byte {
hash256 := sha256.New()
if _, err := hash256.Write(data); err != nil {
fmt.Println(err)
return nil
}
return hash256.Sum(nil)
}
func ReverseString(s string) string {
runes := []rune(s)
for start, end := 0, len(runes)-1; start < end; start, end = start+1, end-1 {
runes[start], runes[end] = runes[end], runes[start]
}
return string(runes)
}
func IsDigitString(s string) bool {
if len(s) == 0 {
return false
}
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
var devTypeMaskMap = map[string]string{
api.Ascend910A5: api.VersionNPU,
}
func MaskDevType(devType string) string {
if masked, ok := devTypeMaskMap[devType]; ok {
return masked
}
return devType
}